import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
import Layout from '@/layout/Index.vue'
import Login from '@/layout/login/Login.vue'
import { useToken } from '@/store/token'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login,
    meta: {
      title: '登录平台',
      icon: '#iconrecipe'
    }
  },
  {
    // 用户布局，所有用户页面整合在 /user 下
    path: '/user',
    component: Layout,
    meta: { requireFromLogin: true },
    redirect: '/user/home',
    children: [
      {
        path: 'home',
        component: () => import('@/system/user/Home.vue'),
        name: 'Home',
        meta: {
          title: '首页',
          icon: '#icondashboard'
        }
      },
      {
        path: 'recipe',
        component: () => import('@/system/user/Recipe.vue'),
        name: 'Recipe',
        meta: {
          title: '美食菜谱',
          icon: '#iconrecipe'
        }
      },
      {
        path: 'RecipeSearch',
        component: () => import('@/system/user/RecipeSearch.vue'),
        name: 'Search',
        meta: {
          title: '美食搜索',
          icon: '#iconrecipe'
        }
      },
      {
        path: 'china-map',
        component: () => import('@/system/user/ChinaMap.vue'),
        name: 'ChinaMap',
        meta: {
          title: '地方特色菜系',
          icon: '#iconrecipe'
        }
      },
      {
        path: 'AiRecipes',
        component: () => import('@/system/user/AiRecipes.vue'),
        name: 'CityRecipes',
        meta: {
          title: 'AI菜谱搜索',
          icon: '#iconrecipe'
        }
      },
      {
        path: 'myNotes',
        component: () => import('@/system/user/MyNotes.vue'),
        name: 'MyNotes',
        meta: {
          title: '我的笔记',
          icon: '#iconrecipe'
        }
      },
      {
        path: 'CommodityList',
        component: () => import('@/system/user/CommodityList.vue'),
        name: 'CommodityList',
        meta: {
          title: '商品购买',
          icon: '#iconrecipe'
        }
      },
      {
        path: 'suggestions',
        component: () => import('@/system/user/Suggestions.vue'),
        name: 'Suggestions',
        meta: {
          title: '意见反馈',
          icon: '#iconrecipe'
        }
      },
      {
        path: 'profile',
        component: () => import('@/system/user/Profile.vue'),
        name: 'Profile',
        meta: {
          title: '个人中心',
          icon: '#iconrecipe'
        },
        redirect: '/user/profile/profile',
        children: [
          {
            path: 'profile',
            component: () => import('@/system/userPage/UserProfile.vue'),
            name: 'UserProfile',
            meta: { title: '个人信息' }
          },
          {
            path: 'feedback',
            component: () => import('@/system/userPage/UserFeedback.vue'),
            name: 'UserFeedback',
            meta: { title: '我的反馈' }
          },
          {
            path: 'recipes',
            component: () => import('@/system/userPage/UserRecipes.vue'),
            name: 'UserRecipes',
            meta: { title: '我的菜谱' }
          },
          {
            path: 'favorites',
            component: () => import('@/system/userPage/UserFavorites.vue'),
            name: 'UserFavorites',
            meta: { title: '我的收藏' }
          },
          {
            path: 'orders',
            component: () => import('@/system/userPage/userOrders.vue'),
            name: 'UserOrders',
            meta: { title: '我的订单' }
          },
          {
            path: 'fgfavorites',
            component: () => import('@/system/userPage/UserFgFavorite.vue'),
            name: 'UserFgFavorites',
            meta: { title: '国外菜谱收藏' }
          }
        ]
      },
    ]
  },
  {
    // 管理员布局
    path: '/admin',
    component: () => import('@/system/admin/layout/Index.vue'),
    meta: {
      requireFromLogin: true,
      title: '管理员页面',
      icon: '#iconrecipe'
    },
    redirect: '/admin/user',
    children: [
      {
        path: 'home',
        component: () => import('@/system/admin/page/adminHome.vue'),
        meta: { title: '首页', icon: '#iconhome' }
      },
      {
        path: 'user',
        component: () => import('@/system/admin/page/adminUser.vue'),
        meta: { title: '用户管理', icon: '#iconuser' }
      },
      {
        path: 'recipe',
        component: () => import('@/system/admin/page/adminRecipe.vue'),
        meta: { title: '菜谱管理', icon: '#iconrecipe' }
      },
      {
        path: 'comment',
        component: () => import('@/system/admin/page/adminComment.vue'),
        meta: { title: '评论管理', icon: '#iconcomment' }
      },
      {
        path: 'carousel',
        component: () => import('@/system/admin/page/adminCarousel.vue'),
        meta: { title: '轮播图管理', icon: '#iconcarousel' }
      },
      {
        path: 'notice',
        component: () => import('@/system/admin/page/adminNotice.vue'),
        meta: { title: '公告管理', icon: '#iconnotice' }
      },
      {
        path: 'product',
        component: () => import('@/system/admin/page/adminProduct.vue'),
        meta: { title: '商品管理', icon: '#icongoods' }
      },
      {
        path: 'order',
        component: () => import('@/system/admin/page/adminOrder.vue'),
        meta: { title: '订单管理', icon: '#iconorder' }
      }
    ]
  },
  // 非布局页面，不受 requireFromLogin 限制
  {
    path: '/register',
    component: () => import('@/layout/login/Register.vue'),
    meta: {
      title: '注册账号',
      icon: '#iconrecipe'
    }
  },
  {
    path: '/comment',
    component: () => import('@/components/comment/Comment.vue'),
    meta: {
      title: '评论',
      icon: '#iconrecipe'
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})


router.beforeEach((to, from, next) => {
  // 如果访问的是 /login 页面，直接放行
  if (to.path === '/login') {
    next();
    return;
  }

  // 获取当前登录状态的 token
  const tokenStore = useToken();
  const token = tokenStore.someState;  // 登录成功后设置 token 存放在这里

  // 检查目标路由的最外层父路由配置是否要求必须从登录页面进入
  if (to.matched[0] && to.matched[0].meta.requireFromLogin) {
    // 如果没有 token（即用户未登录），则重定向到登录页
    if (!token) {
      next('/login');
      return;
    }
    // 如果 token 存在，则放行（不区分是否刷新页面）
    next();
  } else {
    // 对于其他不要求必须从登录页进入的路由，直接放行
    next();
  }
});



export default router
